//
//  StatusCell.swift
//  weibo1990
//
//  Created by 曹晓龙 on 15/10/19.
//  Copyright © 2015年 曹晓龙. All rights reserved.
//

import UIKit
import FFLabel

/// 各控键间距
let statusCellMargin:CGFloat = 12

/// 头像宽度
let statusCellIconWidth:CGFloat = 35

///  label 的网页链接点击
protocol StatusCellLabelDelegaet:NSObjectProtocol{
    
    ///  点击了label中超文本链接
    func labelLinkClick(url:NSURL)
}

/// 微博Cell
class StatusCell: UITableViewCell {

    
    var lblDelegate:StatusCellLabelDelegaet?
    
    /// 单条微博数据视图模型
    var statusViewModel:StatusViewModel?{
        
        didSet{
            
            statusCellTop.statusViewModel = statusViewModel
            
            let text = CXLEmoticonManager.shareEmoticionManager.emticonText(statusViewModel?.status.text ?? "", font: titleLbl.font)
            
            titleLbl.attributedText = text
            
            pictureView.statusViewModel = statusViewModel
            
            //更新配图约束
            pictureView.snp_updateConstraints { (make) -> Void in
                make.width.equalTo(pictureView.bounds.width)
                make.height.equalTo(pictureView.bounds.height)
            }
        }
    }
    //MARK:- 构造函数
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        setupUI()
        
        selectionStyle = UITableViewCellSelectionStyle.None
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    

    /// top
    private lazy var statusCellTop:StatusCellTop = StatusCellTop()
    /// 文字
    lazy var titleLbl:FFLabel = FFLabel(titleText: "策略是", textFont: 15, color: UIColor.darkGrayColor(), screenInset: statusCellMargin)
    
    lazy var pictureView:CXLStatusCellPictureView = CXLStatusCellPictureView()
    /// 底部
    lazy var statusCellBottom:StatusCellBottom = StatusCellBottom()
}

// MARK: - 设置UI
extension StatusCell {
    
    ///  添加UI
    func setupUI(){
    
        // 添加控键
        contentView.addSubview(statusCellTop)
        contentView.addSubview(titleLbl)
        contentView.addSubview(pictureView)
        contentView.addSubview(statusCellBottom)
        // 自动布局
        // top
        statusCellTop.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(contentView.snp_left)
            make.top.equalTo(contentView.snp_top)
            make.right.equalTo(contentView.snp_right)
            make.height.equalTo(statusCellMargin * 2 + statusCellIconWidth)
        }
        
        //titleLbl
        titleLbl.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(contentView.snp_left).offset(statusCellMargin)
            make.top.equalTo(statusCellTop.snp_bottom)
        }
        //底部视图
        statusCellBottom.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(contentView.snp_left)
            make.top.equalTo(pictureView.snp_bottom).offset(statusCellMargin)
            make.right.equalTo(contentView.snp_right)
            make.height.equalTo(44)
        }
        
        //设置代理
        titleLbl.labelDelegate = self
    }
    
    func getCellRowHeight(statusVM:StatusViewModel) ->CGFloat{
        
        statusViewModel = statusVM
        
        layoutIfNeeded()
        
        return CGRectGetMaxY(statusCellBottom.frame)
    }
}

// MARK: - label 的代理方法
extension StatusCell:FFLabelDelegate{
    
    func labelDidSelectedLinkText(label: FFLabel, text: String) {
        
        if text.hasPrefix("http://"){
            lblDelegate?.labelLinkClick(NSURL(string: text)!)
        }
    }
}